#include "IndexAnimation.h"
#include <assert.h>

#define FlipWay (_Way = (AnimationWay)(!_Way))

IndexAnimation::IndexAnimation(int pIndexMin, int pIndexMax, AnimationMode pMode,  int pTickInterval, AnimationWay pWay):
_IndexMin(pIndexMin), _IndexMax(pIndexMax), _Mode(pMode), _TickInterval(pTickInterval) , _Way(pWay)
{
	Reset();
}

void IndexAnimation::Create(int pIndexMin, int pIndexMax, AnimationMode pMode, int pTickInterval, AnimationWay pWay)
{
	_IndexMin = pIndexMin;
	_IndexMax = pIndexMax;
	_Mode = pMode;
	_TickInterval = pTickInterval;
	_Way = pWay;
	Reset();
}

IndexAnimation::IndexAnimation()
{
	Create(0, 0, OneTime, 0);
}

IndexAnimation::~IndexAnimation(void){}

void IndexAnimation::Play()
{
	assert(_IndexMin <= _IndexMax);
	_Play = true;
}

void IndexAnimation::Stop()
{
	_Play = false;
}

void IndexAnimation::Reset()
{
	_Tick = _TickInterval;
	_Index = _Way ? _IndexMin :  _IndexMax;
	_FirstFlip = true;
}

void IndexAnimation::Tick()
{
	if(_Play && !(--_Tick))
	{
		_Tick = _TickInterval;
		_Index += _Way ? 1 : -1;
		if(_Index < _IndexMin || _Index > _IndexMax)
		{
			switch(_Mode)
			{
			// OneTime mode
			case OneTime:
				_Play = false;
				Reset();
				break;

			// Repeat mode
			case Repeat:
				Reset();
				break;

			// OneTimeFlip mode
			case OneTimeFlip:
				if(_FirstFlip)
				{
					_Index = _Way ? _IndexMax :  _IndexMin;
					FlipWay;
				}
				else
				{
					FlipWay;
					Reset();
				}
				break;

			// RepeatFlip mode
			case RepeatFlip:
				_Index = _Way ? _IndexMax :  _IndexMin;
				FlipWay;
				break;
			}
		}
	}
}






